#include "stdafx.h"
#include "xAttribute.h"

namespace ck
{
	namespace xml
	{
		Attribute::Attribute(xmlNodePtr pxNode):
		Node(pxNode)
		{
			m_value.clear();
		}


		Attribute::~Attribute()
		{
		}

		const char* Attribute::GetName()
		{
			if (m_name.empty())
			{
				//const char* name = (const char*)(m_pxNode->name);
				//const char* name_gbk = auge_encoding_convert("UTF-8", "GBK", name, strlen(name));
				//m_name = name_gbk;
				m_name = (const char*)(m_pxNode->name);
			}

			return m_name.c_str();
		}

		const char* Attribute::GetValue()
		{
			if (!m_value.empty())
			{
				return m_value.c_str();
			}

			const xmlChar* ns_uri = NULL;
			if (m_pxNode->ns != NULL)
			{
				ns_uri = m_pxNode->ns->href;
			}

			xmlChar *value = xmlGetNsProp(m_pxNode->parent, m_pxNode->name, ns_uri);
			if (value == NULL)
			{
				return NULL;
			}
			//const char* value_gbk = auge_encoding_convert("UTF-8", "GBK", (char*)value, strlen((char*)value));
			//xmlFree(value);
			//m_value = value_gbk;
			//return m_value.c_str();

			m_value = (char*)value;
			return m_value.c_str();
		}

		void Attribute::SetValue(const char* value)
		{
			if (value != NULL)
			{
				//const char* value_utf8 = auge_encoding_convert("GBK", "UTF-8", value, strlen(value));
				//xmlSetProp(m_pxNode->parent, m_pxNode->name, (xmlChar*)value_utf8);

				xmlSetProp(m_pxNode->parent, m_pxNode->name, (xmlChar*)value);
			}
		}
	}
}

